﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Providers;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace NLBS.Web.UI
{
    /// <summary>
    /// Summary description for RichTextEditorCtl.
    /// </summary>
    [ValidationProperty("Text")]
    public class RichTextEditor : WebControl, INamingContainer
    {
        #region EventHandlers
        public delegate void ErrorEventHandler(object sender, RichTextEditorErrorEventArgs e);
        public event ErrorEventHandler Error;

        public void OnError(Exception ex)
        {
            if (Error != null)
            {
                Error(this, new RichTextEditorErrorEventArgs(ex));
            }
        }
        #endregion

        private System.Web.UI.Control editor;
        private BlogEntryEditorProvider provider;

        private Unit _height = Unit.Empty;
        private Unit _width = Unit.Empty;

        public string Text
        {
            get
            {
                return provider.Text;
            }
            set
            {
                provider.Text = value;
            }
        }

        public override Unit Height
        {
            get
            {
                return _height;
            }
            set
            {
                _height = value;
            }
        }

        public override Unit Width
        {
            get
            {
                return _width;
            }
            set
            {
                _width = value;
            }
        }


        public string Xhtml
        {
            get
            {
                return provider.Xhtml;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            try
            {
                provider = BlogEntryEditorProvider.Instance();
                provider.ControlId = this.ID;
                provider.InitializeControl();

                if (_height != Unit.Empty)
                    provider.Height = _height;
                if (_width != Unit.Empty)
                    provider.Width = _width;

                editor = provider.RichTextEditorControl;
                this.Controls.Add(editor);
                base.OnInit(e);
            }
            catch (ArgumentNullException ex)
            {
                OnError(ex);
            }
            catch (InvalidOperationException ex)
            {
                OnError(ex);
            }
            catch (UnauthorizedAccessException ex)
            {
                OnError(ex);
            }
        }
    }

    public class RichTextEditorErrorEventArgs : EventArgs
    {
        private Exception _ex;
        public RichTextEditorErrorEventArgs(Exception ex)
        {
            _ex = ex;
        }

        public Exception Exception
        {
            get
            {
                return _ex;
            }
            set
            {
                _ex = value;
            }
        }
    }
}
